package cn.wsn2018.tools.interceptor;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.model.MySessionContext;
import cn.wsn2018.tools.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by NZX on 2018/12/18.
 */
public class SessionInterceptor implements HandlerInterceptor{
    private static final Logger logger = LoggerFactory.getLogger(SessionInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String sessionId = request.getParameter("token");
        if(sessionId!=null&&!sessionId.isEmpty()){
            MySessionContext myc= MySessionContext.getInstance();
            HttpSession session = myc.getSession(sessionId);
            if(session!=null&&session.getAttribute("username")!=null&&session.getAttribute("role")!=null){
                session.setMaxInactiveInterval(7200);
                myc.addSession(session);
                return true;
            }else {
                String json = JsonUtil.failedJson(CommonValue.JSON_NOT_LOGIN);
                ServletUtil.getInstance().responseOperating(response,json);

                String username = request.getParameter("username");
                String token = request.getParameter("token");
                String warningLevel= CommonValue.WARNING_LEVEL_1;

                if(!StringUtil.isEmptyString(username)){
                    if(!StringUtil.isEmptyString(token)){
                        warningLevel=CommonValue.WARNING_LEVEL_0;
                    }
                    LogUtil.produceWarningLogs(
                            username,
                            CommonValue.WARNING_TYPE_0,
                            warningLevel,
                            CommonValue.LOGINMODULE,
                            ServletUtil.getClientIp(request),
                            LogUtil.produceWarningMsg(CommonValue.WARNING_TYPE_0,username, null,CommonValue.LOGMODULE));
                }
                return false;
            }
        }else {
            String json = JsonUtil.failedJson(CommonValue.JSON_NOT_LOGIN);
            ServletUtil.getInstance().responseOperating(response,json);

            String username = request.getParameter("username");
            String token = request.getParameter("token");
            String warningLevel= CommonValue.WARNING_LEVEL_1;
            if(!StringUtil.isEmptyString(username)){
                if(!StringUtil.isEmptyString(token)){
                    warningLevel=CommonValue.WARNING_LEVEL_0;
                }
                LogUtil.produceWarningLogs(
                        username,
                        CommonValue.WARNING_TYPE_0,
                        warningLevel,
                        CommonValue.LOGINMODULE,
                        ServletUtil.getClientIp(request),
                        LogUtil.produceWarningMsg(CommonValue.WARNING_TYPE_0,username,null,CommonValue.LOGMODULE));
            }
            return false;
        }
    }
}
